package com.a1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class InventoryManagementGUI extends JFrame {
    private  InventoryManagement im;
    private  JTextField productNameField;
    private  JTextField quantityField;
    private  JButton addButton;
    private  JButton queryButton;
    private  JButton updateButton;

    private  JButton deldateButton;

    public InventoryManagementGUI() {
        im = new InventoryManagement();

        // 设置窗口标题
        setTitle("库存管理系统");
        // 设置窗口大小
        setSize(400, 300);
        // 设置窗口关闭时的操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗口布局
        setLayout(new GridLayout(4, 2));

        // 创建商品名称标签和输入框
        JLabel productNameLabel = new JLabel("商品名称:");
        productNameField = new JTextField();
        // 创建数量标签和输入框
        JLabel quantityLabel = new JLabel("数量:");
        quantityField = new JTextField();

        // 创建添加、查询和更新按钮
        addButton = new JButton("添加商品");
        queryButton = new JButton("查询库存");
        updateButton = new JButton("更新库存");
        deldateButton= new JButton("删除库存");

        // 将组件添加到窗口
        add(productNameLabel);
        add(productNameField);
        add(quantityLabel);
        add(quantityField);
        add(addButton);
        add(queryButton);
        add(updateButton);
        add(deldateButton);


         //为添加按钮添加事件监听器
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String productName = productNameField.getText();
                try {
                    int quantity = Integer.parseInt(quantityField.getText());
                    im.addProduct(productName, quantity);
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null, "请输入有效的数量！");
                }
            }
        });

        // 为查询按钮添加事件监听器
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String productName = productNameField.getText();
                im.queryInventory(productName);
            }
        });

        // 为更新按钮添加事件监听器
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String productName = productNameField.getText();
                try {
                    int newQuantity = Integer.parseInt(quantityField.getText());
                    im.updateInventory(productName, newQuantity);
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null, "请输入有效的数量！");
                }
            }
        });
    }

    public static void main(String[] args) {
        // 在事件调度线程中创建和显示 GUI
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                InventoryManagementGUI gui = new InventoryManagementGUI();
                gui.setVisible(true);
            }
        });
    }
}